package com.nnnu.wsnackshop.controller.user;

import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.CategoryPageDTO;
import com.nnnu.wsnackshop.pojo.vo.CategoryVO;
import com.nnnu.wsnackshop.result.PageResult;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.ICategoriesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.List;

/**
 * <p>
 * 用户端-商品分类 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("userCategoriesController")
@RequestMapping("/api/user/categories")
@Tag(name = "用户端-分类接口")
@Slf4j
@RequiredArgsConstructor
@Validated
@CacheConfig(cacheNames = "categories")
public class CategoriesController {
    private final ICategoriesService categoryService;

    @Operation(summary = "分页查询分类", description = "分页查询所有分类")
    @GetMapping("/page")
    @Cacheable(key = "'page:' + #dto.pageNum + ':' + #dto.pageSize")
    public Result<PageResult<CategoryVO>> pageCategories(@ParameterObject CategoryPageDTO dto) {
        log.info("[用户端] 分页查询分类：{}", dto);
        PageResult<CategoryVO> result = categoryService.pageCategories(dto);
        return Result.success(result, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取分类详情", description = "根据ID获取分类详细信息")
    @GetMapping("/{id}")
    @Cacheable(key = "'detail:' + #id")
    public Result<CategoryVO> getCategoryDetail(
            @PathVariable @Parameter(description = "分类ID", in = ParameterIn.PATH) Integer id) {
        log.info("[用户端] 获取分类详情：{}", id);
        CategoryVO vo = categoryService.getCategoryDetail(id);
        return Result.success(vo, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取顶级分类", description = "获取所有顶级分类（父分类为空的分类）")
    @GetMapping("/top")
    @Cacheable(key = "'top'")
    public Result<List<CategoryVO>> getTopCategories() {
        log.info("[用户端] 获取顶级分类");
        List<CategoryVO> categories = categoryService.listTopCategories();
        return Result.success(categories, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取子分类", description = "获取指定分类的所有子分类")
    @GetMapping("/{parentId}/children")
    @Cacheable(key = "'children_' + #parentId")
    public Result<List<CategoryVO>> getChildCategories(
            @PathVariable @Parameter(description = "父分类ID", in = ParameterIn.PATH) Integer parentId) {
        log.info("[用户端] 获取子分类，父分类ID：{}", parentId);
        List<CategoryVO> categories = categoryService.listChildCategories(parentId);
        return Result.success(categories, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取分类树", description = "获取完整的分类树结构")
    @GetMapping("/tree")
    @Cacheable(key = "'tree'")
    public Result<List<CategoryVO>> getCategoryTree() {
        log.info("[用户端] 获取分类树");
        List<CategoryVO> tree = categoryService.getCategoryTree();
        return Result.success(tree, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取所有分类", description = "获取所有分类的平铺列表")
    @GetMapping("/list")
    @Cacheable(key = "'all'")
    public Result<List<CategoryVO>> getAllCategories() {
        log.info("[用户端] 获取所有分类");
        List<CategoryVO> categories = categoryService.listAllCategories();
        return Result.success(categories, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "清理分类缓存", description = "清理所有分类相关的缓存")
    @PostMapping("/cache/clear")
    @CacheEvict(value = "categories", allEntries = true)
    public Result<String> clearCache() {
        log.info("[用户端] 清理分类缓存");
        return Result.success("缓存清理成功", "缓存清理成功");
    }
}
